/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include "sle_ssap_server_impl.h"
#include "cmd_input.h"
#include <vector>
#include <memory>
#include <cstring>
#include <iterator>

namespace OHOS {
namespace NearLink {
namespace SleStandard {
namespace {
using namespace testing::ext;
static SleSsapServer *g_ssapServers;
} // namespace

class SsapsHandlerTest : public testing::Test {
public:
    static void SetUpTestCase();
    static void TearDownTestCase();
    void SetUp();
    void TearDown();
};

void SsapsHandlerTest::SetUpTestCase() {}

void SsapsHandlerTest::TearDownTestCase() {}

void SsapsHandlerTest::SetUp() {
    SleUuid uuid;
    uuid.len = 1;
    uuid.uuid[0] = 1;
    g_ssapServers = new SleSsapServer(uuid);
}

void SsapsHandlerTest::TearDown() {}

HWTEST_F(SsapsHandlerTest, AddServiceTest001, TestSize.Level1)
{
    uint16_t serviceHandle;
    SleUuid serviceUuid;
    // memcpy(serviceUuid.uuid, "sleToolService", strlen("sleToolService"));
    // strcpy(serviceUuid.uuid, "sleToolService");
    // serviceUuid.len = strlen("sleToolService");
    serviceUuid.len = 1;
    serviceUuid.uuid[0] = 0;
    bool isPrimary = true;
    SleErrorCode ret = g_ssapServers->AddService(serviceUuid, isPrimary, serviceHandle);
    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

HWTEST_F(SsapsHandlerTest, AddPropertyTest001, TestSize.Level1)
{
    SsapsPropertyParam param = {
        .permissions = SSAP_PERMISSION_CAN_READ | SSAP_PERMISSION_CAN_WRITE,
        .opIndication = SSAP_OPERATEIND_BIT_WRITE | SSAP_OPERATEIND_BIT_NOTIFY,

    };
    uint16_t propertyHandle;
    // memcpy(param.uuid, "sleToolProp", strlen("sleToolProp"));
    // strcpy(param.uuid, "sleToolService");
    // param.uuid.len = strlen("sleToolProp");
    param.uuid.len = 1;
    param.uuid.uuid[0] = 1;
    uint16_t serviceHandle = (uint8_t)0;
    param.permissions = (uint8_t)0;
    param.opIndication = (uint8_t)0;
    SleErrorCode ret = g_ssapServers->AddProperty(serviceHandle, param, propertyHandle);

    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

HWTEST_F(SsapsHandlerTest, AddDescriptorTest001, TestSize.Level1)
{
    const char *valueStr = "SleTestProperty";
    SsapsDescriptorParam descParam = {
        .permissions = SSAP_PERMISSION_CAN_READ | SSAP_PERMISSION_CAN_WRITE,
        .operateInd = SSAP_OPERATEIND_BIT_WRITE_NO_RSP,
        .type = SSAP_DESCRIPTOR_TYPE_USER_DESCRIPTION_TYPE,
        .valueLen = strlen(valueStr),
        .value = new uint8_t[descParam.valueLen + 1],
    };
    descParam.uuid.len = 1;
    descParam.uuid.uuid[0] = 1;
    uint16_t serviceHandle = 0;
    uint16_t propertyHandle = 0;
    descParam.permissions = (uint8_t)0;
    descParam.operateInd = (uint8_t)0;
    SleErrorCode ret = g_ssapServers->AddDescriptor(serviceHandle, propertyHandle, descParam);
    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

HWTEST_F(SsapsHandlerTest, AddSsapServerTest001, TestSize.Level1)
{
    SleErrorCode ret = g_ssapServers->RemoveAllServices();
    EXPECT_EQ(ret, SLE_ERR_FAILED);
}

// HWTEST_F(SsapsHandlerTest, RemoveAllServicesTest001, TestSize.Level1)
// {
//     uint16_t serviceHandle = (uint8_t)SleGetInputNumber("Please input serviceHandle:");
//     SleErrorCode ret = g_ssapServers->StartService(serviceHandle);
//     EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
// }

HWTEST_F(SsapsHandlerTest, SendResponseTest001, TestSize.Level1)
{
    uint16_t connectId = (uint16_t)255;
    SsapsSendRspParam param;
    param.requestId = (uint16_t)124;
    param.status = 0;
    uint8_t value[256];
    const char* str = "nearlink test";
    memcpy(value, str, sizeof(value));
    param.value = value;
    param.valueLen = strlen(reinterpret_cast<const char*>(value)) + 1;
    SleErrorCode ret = g_ssapServers->SendResponse(connectId, param);
    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

HWTEST_F(SsapsHandlerTest, NotifyIndicateTest001, TestSize.Level1)
{
    uint16_t connectId = (uint16_t)255;
    SsapsNotifyParam param;
    param.handle = (uint16_t)0;
    param.type = (SsapPropertyType)0;
    uint8_t value[256];
    const char* str = "nearlink test";
    memcpy(value, str, sizeof(value));
    param.value = value;
    param.valueLen = strlen(reinterpret_cast<const char*>(value)) + 1;
    SleErrorCode ret = g_ssapServers->NotifyIndicate(connectId, param);
    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

// HWTEST_F(SsapsHandlerTest, NotifyIndicateByUuidTest001, TestSize.Level1)
// {
//     uint16_t connectId = (uint16_t)255;
//     SsapsNotyfyByUuidParam param;
//     param.uuid.len = 0;
//     param.uuid.uuid[0] = 1;
//     param.startHandle = (uint16_t)0;
//     param.endHandle = (uint16_t)0;
//     param.type = (SsapPropertyType)0;
//     uint8_t value[256];
//     SleGetInputString("Please input value(<255):", reinterpret_cast<char*>(value), 256);
//     param.value = value;
//     param.valueLen = strlen(reinterpret_cast<const char*>(value)) + 1;
//     SleErrorCode ret = g_ssapServers->NotifyIndicateByUuid(connectId, param);
//     EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
// }

HWTEST_F(SsapsHandlerTest, SetServerMtuInfoTest001, TestSize.Level1)
{
    SsapMtuInfo mtuInfo;
    mtuInfo.mtuSize = (uint32_t)256;
    SleErrorCode ret = g_ssapServers->SetServerMtuInfo(mtuInfo);
    EXPECT_EQ(ret, SLE_ERR_FAILED);
}
}
} // namespace NetManagerStandard
} // namespace OHOS